#!/bin/bash
#author: <fanyuantao>
#date: 2024-08-08
#desc: This script is used to check the disk usage of all partitions.

THRESHOLD=80

function check_disk_usage() {
  echo "The disk usage is over $THRESHOLD%:"
  echo ""
  df -H | tail -n +2 | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  do
    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
    partition=$(echo $output | awk '{ print $2 }' )
    if [ $usep -ge $THRESHOLD ]; then
      echo "Warning: The partition \"$partition\" has used $usep% at $(date)"
    fi
  done
}

function print_disk_info() {
  echo "The disk usage is as follows:"
  echo ""
  df -h
}

function main() {
  print_disk_info
  check_disk_usage
}

main